<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Organizations
 *
 * @author Zoila
 */
class OrganizationsController extends AppController {
    //put your code here
    public $uses = array('Organization','Member','OrgMember');
    public $helpers= array('Html','Form');
    
     //AUTORIZACION
    public function isAuthorized($user) {
        // All registered users can add posts
        if (in_array($this->action, array('add', 'view'))) {
            return true;
        }

        // The owner of a post can edit and delete it
        if (in_array($this->action, array('edit', 'delete'))) {
            $orgId = $this->request->params['pass'][0];
            if ($this->Organization->isOwnedBy($orgId, $user['id'])) {
                return true;
            }
        }

        return parent::isAuthorized($user);
    }
    
     
     public function add(){
        if ($this->request->is('post')){  //ZP: se verifica en con CakeRequest::is() si es un HTTP POST request
            $this->request->data['Organization']['creator_id'] = $this->Auth->user('id'); //Added this line
            $this->request->data['Organization']['active'] =true;
            if($this->Organization->save($this->request->data)){
                $orgMember= array('organization_id'=> $this->Organization->id,
                         'user_id'=> $this->Auth->user('id'),
                         'state' => 1);
                    
                    if($this->OrgMember->save($orgMember)){
                        $this->Session->setFlash('Your organization has been saved.');
                        $this->redirect(array('controller'=>'organizations','action'=>'index'));
                    }
                
                
            }
            else{
                $this->Session-> setFlash('Unable to add your organization');
            }
        }
    }
    public function edit($id = null) {
        $this->Organization->id = $id;
        if ($this->request->is('get')) {
            $this->request->data = $this->Organization->read();
        } else {
            if ($this->Organization->save($this->request->data)) {
                $this->Session->setFlash('Your organization has been updated.');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Unable to update your organization.');
            }
        }
    }
    public function delete($id) {
        $this->Organization->id = $id;
        $org= $this->Organization->read();
        $org['Organization']['active']=false;
        if ($this->request->is('get')) {
            throw new MethodNotAllowedException();
        }
        if ($this->Organization->save($org)) {
            $this->Session->setFlash('The organization with id: ' . $id . ' has been deleted.');
            $this->redirect(array('action' => 'index'));
        }
    }
    
    public function index(){ // ZP: solo puedo ver mis organizaciones
        if ($this->Auth->user()!=NULL){
            $this->set('orgs',  $this->Organization->find('all',array('conditions' => array('Organization.creator_id =' => $this->Auth->user('id')))));

            $this->OrgMember->unbindModel(
                    array('belongsTo' => array('User'))
            );
            
            $orgMembers=$this->OrgMember->find('all',array('conditions' => array('OrgMember.user_id ='=>$this->Auth->user('id'),'OrgMember.state !=' => 1)));
            
            $orgInv=array();
            
            foreach($orgMembers as $orgMember ){
                $org=$orgMember['Organization'];
                 array_push($orgInv,$org);
            }
            $this->set('orgsIv', $orgInv);
//            var_dump($orgInv);

        }
    }
    
    public function view($id = null) {
        
        $this->Organization->id = $id;
        $org=$this->Organization->read();
        if ($this->Auth->user()!=NULL ){

           
            $this->Organization->unbindModel(
                    array('hasMany' => array('Team','OrgMember'))
            );
            
            $isInvit=false;
            $staff=array();
            foreach ($org['OrgMember'] as $orgI){
                $this->OrgMember->unbindModel(
                    array('belongsTo' => array('Organization'))
                );
                if($orgI['user_id']==$this->Auth->user('id')){
                    $isPartOf=true;
                    if($orgI['state']==0) $isInvit=true;
                }
                $orgI= $this->OrgMember->find('first',array('conditions' => array('OrgMember.organization_id ='=>$orgI['organization_id'],'OrgMember.user_id =' => $orgI['user_id'])));
                
                array_push($staff,$orgI);
            }
            //var_dump($staff);
            if ($isPartOf){
                $this->set('staff',$staff );
                $this->set('org',$org );
                $this->set('isInvit',$isInvit);
                $org=$this->Organization->read();
                $this->Session->write('Organization',$org);
               // $this->Session->delete('Organization.Project');
            }
        }
    }
    
    
}

?>
